package ar.edu.unlp.info.cdiv.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import ar.edu.unlp.info.cdiv.Constants;
import ar.edu.unlp.info.cdiv.model.Album;
import ar.edu.unlp.info.cdiv.model.Persona;
import ar.edu.unlp.info.cdiv.service.PersonService;

@Controller
@RequestMapping(value = "/imagen")
public class ImagenController extends AbstractController{

	@Autowired
	PersonService service;
	
	@RequestMapping("{id}/perfil")
	public void viewProfileImage(@PathVariable Long id,HttpServletResponse response){
		Persona persona = (Persona)service.get(Persona.class, id);	
		this.showImage(this.getWebpFolder()+File.separator+persona.getUser().getUsername()+File.separator+"foto.webp", response);
	}
	
	@RequestMapping(value = "ver/{usuario}/{album}/{id_album}/{foto}")
	public String verFoto(Model model,@PathVariable String usuario,@PathVariable String album,@PathVariable Long id_album, @PathVariable String foto,HttpServletResponse response){
		Album anAlbum = (Album)service.get(Album.class, id_album);
		int index = anAlbum.getFotos().indexOf(foto+".webp");
		if (index > 0){
			model.addAttribute(Constants.FOTO_ANTERIOR,anAlbum.getFotos().get(index-1));
		}
		if (index < anAlbum.getFotos().size() - 1){
			model.addAttribute(Constants.FOTO_POSTERIOR,anAlbum.getFotos().get(index+1));
		}
		model.addAttribute(Constants.ALBUM, anAlbum);
		model.addAttribute(Constants.FOTO, foto);
		model.addAttribute("usuario", usuario);
		return "foto";
	}
	
	@RequestMapping("{usuario}/{album}/{foto}")
	public void verImagen(@PathVariable String usuario,@PathVariable String album,@PathVariable String foto, HttpServletResponse response) {
		this.showImage(this.getWebpFolder()+File.separator+usuario+File.separator+album+File.separator+foto+".webp", response);
	}
	
	

	public void showImage(String path, HttpServletResponse response){
		File file = new File(path);
		response.setContentType("image/webp");
		OutputStream output = null;
		FileInputStream input = null;
		try {
			output = response.getOutputStream();
			input = new FileInputStream(file);
			byte[] buffer = new byte[(int) file.length()];
			input.read(buffer);
			response.setContentLength(buffer.length);
			output.write(buffer);
		} catch (Exception e) {
			System.out.println();
		} finally {
			try {
				input.close();
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@RequestMapping(value = "explorar")
	public String explorar(Model model, HttpServletRequest request){
		//TODO buscar albums de manera random
		request.getSession().setAttribute(Constants.SECCION,Constants.EXPLORAR);
		List<Album> albums = (List<Album>)service.getAll(Album.class);
		model.addAttribute(Constants.ALBUMS,albums);
		return "explorar";
	}
	
}
